<?php
namespace My\Controller;

use Utuo\BackgroundController;

class DynaFormDataController extends BackgroundController {
	
	private $formId = 0;
	
	protected  $verify = array(
		'0' => "未审核",
		'1' => "审核通过",
		'3' => "审核未通过",
	);
	
	public function _initialize() {
		$this->formId = I('form_id', 0, "intval");
		$this->setControllerTitle('表单管理');
		$this->setActionTitle('modify', '编辑与查看', "form_id=".$this->formId);
		$this->setActionTitle('index', '提交内容列表', "form_id=".$this->formId);
		$this->setActionTitle('recycle', '回收站', "form_id=".$this->formId);
		$this->setActionNav(array(
				'index',
				'recycle' 
		));
	}
	
	public function indexAction() {
		$this->getLists( 0 );
	}
	
	public function recycleAction() {
		$this->getLists( 1 );
	}
	
	// 显示用户提交列表
	private function getLists($is_deleted) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$formId = $this->formId;
		$dynaForm = M('DynaForm')->find($formId);
		$dynaForm and $dynaForm['config'] = unserialize($dynaForm["config"]);
		$dynaFormDataLists = M("DynaFormData")->where("form_id=%d and is_deleted=%d", $formId, $is_deleted)->limit($pagesize)->page($page)->order('id desc')->select();
		$dynaFormDataCount = M("DynaFormData")->where("form_id=%d and is_deleted=%d", $formId, $is_deleted)->count();
		foreach ($dynaFormDataLists as &$v) {
			$v['verifyOptions'] = $this->getOption($this->verify, $v['verify'], null);
			$v['data'] = unserialize($v['data']);
		}
		$this->showPage($dynaFormDataCount, $pagesize);
		$this->assign("dynaFormDataLists", $dynaFormDataLists);
		$this->assign("dynaForm", $dynaForm);
		$this->display();
	}
	
	// 表单修改
	public function modifyAction() {
		$id = I("id", 0, "intval");
		$dynaFormData = D("DynaFormData")->find($id) or $this->ajaxError("提交数据不存在");
		$dynaForm = D("DynaForm")->find($dynaFormData['form_id']);
		$dynaForm and $dynaForm['config'] = unserialize($dynaForm['config']);
		$dynaFormData and $dynaFormData['data'] = unserialize($dynaFormData['data']);
		foreach ($dynaForm['config'] as $k => &$v) {
			$v['varValue'] = $dynaFormData['data'][$v['varName']];
		}
		$this->assign("dynaFormData", $dynaFormData);
		$this->assign("dynaForm", $dynaForm);
		$this->display();
	}
	
	public function saveAction() {
		$m = M("DynaFormData");
		$id = I("id", 0, "intval"); // data id
		$dynaFormData = D("DynaFormData")->find($id) or $this->ajaxError("提交数据不存在");
		$dynaForm = M("DynaForm")->find($dynaFormData['form_id']) or $this->ajaxError("表单不存在");
		$config = unserialize($dynaForm['config']);
		$submitData = array();
		foreach ($config as $k => $v) {
			$varVal = I($v['varName'], "");
			$submitData[$v['varName']] = $varVal;
		}
		false !== M("DynaFormData")->save(array(
				'id' => $id,
				'data' => serialize($submitData),
		)) and $this->ajaxSuccess("提交成功");
		$this->ajaxSuccess('提交失败');
	}
 	
	// 设置状态
	public function setReadStatusAction() {
		$this->ajaxSetStatus("DynaFormData");
	}
	
	// 移除
	public function removeAction() {
		$this->ajaxRemove("DynaFormData");
	}
	
	// 恢复
	public function restoreAction() {
		$this->ajaxRestore("DynaFormData");
	}
	
	// 删除
	public function deleteAction() {
		$this->ajaxDelete("DynaFormData");
	}
	
	// 设置审核状态
	public function setVerifyStatusAction() {
		$id = I('id',0, 'intval');
		$verify = I('verify', 0, 'intval');
		false !== M("DynaFormData")->where(array('id'=>$id))->setField('verify', $verify) and $this->ajaxSuccess("设置成功 ");
		$this->ajaxSuccess("设置失败");
	}
	
}

?>